package com.irdstudio.efp.flow.service.chart;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.flow.common.constance.EventChartConstance;
import com.irdstudio.efp.flow.service.domain.BizEventChartInfo;
import com.irdstudio.efp.flow.service.domain.BizFlowProcess;
import com.irdstudio.efp.flow.service.domain.BizFlowRoute;
import com.irdstudio.basic.framework.core.util.StringUtil;

public class EventChartParser {
	
	/**
	 * 转换为json对象
	 * @param chartJson
	 * @return Map<resourceId, JSONObject>
	 */
	public static EventChartParserInfo findRouterInfo(BizEventChartInfo chartInfo) {
		//模型信息
		JSONObject modelInfo = JSONObject.parseObject(chartInfo.getEventChartJson());
		Map<String, JSONObject> shapeMap = parseChartToMap(modelInfo);
		
		//节点信息
		List<BizFlowProcess> processList = new ArrayList<>();
		//路由信息
		List<BizFlowRoute> routeList = new ArrayList<>();
		
		//分析路由信息
		Iterator<String> shapeKeyIt = shapeMap.keySet().iterator();
		while(shapeKeyIt.hasNext()) {
			JSONObject shape = shapeMap.get(shapeKeyIt.next());
			//模具信息
			JSONObject stencil = shape.getJSONObject("stencil");
			//模具ID
			String stencilId = stencil.getString("id");
			//判断节点
			if(StringUtil.isNotEmpty(stencilId) && stencilId.equals(EventChartConstance.STENCIL_TYPE_UserTask)) {
				//属性信息
				JSONObject properites = shape.getJSONObject("properties");
				String nodeId = properites.getString("overrideid"); //节点ID
				String nodeName = properites.getString("name"); //节点名称
				String bizNodeDutyNo = properites.getString("biznodedutyno");
				String bizNodeDutyName = properites.getString("biznodedutyname");
				String maxAprvNumStr = properites.getString("maxaprvnum");
				
				int maxAprvNum = 0;
				if(StringUtil.isNotEmpty(maxAprvNumStr)) {
					maxAprvNum = Integer.valueOf(maxAprvNumStr);
				}
				
				//创建节点信息
				BizFlowProcess processInfo = new BizFlowProcess();
				processInfo.setBizNodeId(nodeId);
				processInfo.setBizNodeName(nodeName);
				processInfo.setBizNodeDutyNo(bizNodeDutyNo);
				processInfo.setBizNodeDutyName(bizNodeDutyName);
				processInfo.setMaxAprvNum(maxAprvNum);
				processList.add(processInfo);
				
				//节点指向信息
				JSONArray outgoingArr = shape.getJSONArray("outgoing");
				if(outgoingArr != null && outgoingArr.size() > 0) {
					for(int j=0; j<outgoingArr.size(); j++) {
						//指向信息
						JSONObject outgoing = outgoingArr.getJSONObject(j);
						//目标图形ID
						String targetId = outgoing.getString("resourceId");
						
						//流程线信息
						JSONObject sequenceFlowShape = shapeMap.get(targetId);
						//属性信息
						JSONObject seqFlowProperites = sequenceFlowShape.getJSONObject("properties");
						String bizRouteName = seqFlowProperites.getString("name");
						String bizRouteRule = seqFlowProperites.getString("bizrouterule");
						String remarks = seqFlowProperites.getString("remarks");
						//流程线模具信息
						JSONObject sequenceStencil = sequenceFlowShape.getJSONObject("stencil");
						//流程线模具ID
						String sequenceStencilId = sequenceStencil.getString("id");
						
						if(StringUtil.isNotEmpty(sequenceStencilId) && sequenceStencilId.equals(EventChartConstance.STENCIL_TYPE_SequenceFlow)) {
							//查找目标节点
							JSONObject targetShape = findNextUserTask(shapeMap, targetId);
							if(targetShape != null) {
								//属性信息
								JSONObject endProperites = targetShape.getJSONObject("properties");
								String endNodeId = endProperites.getString("overrideid"); //节点ID
								String endNodeName = endProperites.getString("name"); //节点名称
								
								//创建路由信息
								BizFlowRoute routeInfo = new BizFlowRoute();
								routeInfo.setBizFlowId(chartInfo.getBizFlowId());
								routeInfo.setBizEventId(chartInfo.getBizEventId());
								routeInfo.setBizSourceNodeId(nodeId);
								routeInfo.setBizSourceNodeName(nodeName);
								routeInfo.setBizTargetNodeId(endNodeId);
								routeInfo.setBizTargetNodeName(endNodeName);
								routeInfo.setBizRouteName(bizRouteName);
								routeInfo.setBizRouteRule(bizRouteRule);
								routeInfo.setRemarks(remarks);
								routeList.add(routeInfo);
							}
						}
					}
				}
			}
		}
		
		//事件图形信息
		EventChartParserInfo chartParserInfo = new EventChartParserInfo();
		chartParserInfo.setProcessList(processList);
		chartParserInfo.setRouteList(routeList);
		return chartParserInfo;
	}
	
	/**
	 * 转为Map
	 * @return
	 */
	private static Map<String, JSONObject> parseChartToMap(JSONObject modelInfo) {
		Map<String, JSONObject> shapeMap = new HashMap<String, JSONObject>();
		//事件流图形节点信息
		JSONArray shapeArr = modelInfo.getJSONArray("childShapes");
		for(int i=0; i<shapeArr.size(); i++) {
			//图形信息
			JSONObject shape = shapeArr.getJSONObject(i);
			//图形ID
			String resourceId = shape.getString("resourceId");
			//缓存图形信息
			shapeMap.put(resourceId, shape);
		}
		
		return shapeMap;
	}
	
	/**
	 * 找到下个用户处理节点
	 * @param shapeMap
	 * @param resourceId
	 * @return
	 */
	private static JSONObject findNextUserTask(Map<String, JSONObject> shapeMap, String resourceId) {
		JSONObject shape = shapeMap.get(resourceId);
		//模具信息
		JSONObject stencil = shape.getJSONObject("stencil");
		//模具ID
		String stencilId = stencil.getString("id");
		//判断节点
		if(StringUtil.isNotEmpty(stencilId) && stencilId.equals(EventChartConstance.STENCIL_TYPE_UserTask)) {
			//是用户节点，直接返回
			return shape;
		} else {
			//节点指向信息
			JSONArray outgoingArr = shape.getJSONArray("outgoing");
			if(outgoingArr != null && outgoingArr.size() > 0) {
				//指向信息
				JSONObject outgoing = outgoingArr.getJSONObject(0);
				//目标图形ID
				String targetId = outgoing.getString("resourceId");
				
				return findNextUserTask(shapeMap, targetId);
			}
		}
		
		return null;
	}
}
